﻿using Dal;
using Entity;
using Entity.DTO;
using Entity.ViewModel.OrderMaterial;
using IDal;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Principal;

namespace Bll
{
    /// <summary>
    /// 订单材料业务逻辑层
    /// </summary>
    public class OrderMaterialBll : IOrderMaterialBll
    {
        IOrderMaterialDal _orderMaterialDal;
        IUserInfoDal _userInfoDal;
        IOrderDal _orderDal;
        IMaterialDal _materialDal;
        IClassificationDal _classificationDal;


        public OrderMaterialBll(IOrderMaterialDal orderMaterialDal, IUserInfoDal userInfoDal, IOrderDal orderDal, IMaterialDal materialDal, IClassificationDal classificationDal)
        {
            _orderMaterialDal = orderMaterialDal;
            _userInfoDal = userInfoDal;
            _orderDal = orderDal;
            _materialDal = materialDal;
            _classificationDal = classificationDal;
        }


        /// <summary>
        /// 创建订单材料
        /// </summary>
        /// <returns></returns>
        public bool CreateOrderMaterial(List<CreateOrderMaterialModel> model, out string msg)
        {
            foreach (var item in model)
            {
                OrderMaterial entity = new OrderMaterial();
                //创建guid 赋值给订单材料id，目的是保证id唯一
                entity.Id = Guid.NewGuid().ToString();
                entity.OrderId = item.OrderId;
                entity.MaterialId = item.MaterialId;
                entity.Num = item.Num;

                //调用数据访问层创建订单材料
                bool isSuccess = _orderMaterialDal.CreateEntity(entity);
            }

            msg = "成功";

            return true;
        }


        /// <summary>
        /// 软删除订单材料
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteOrderMaterial(string id)
        {
            OrderMaterial orderMaterial = _orderMaterialDal.GetDbSet().FirstOrDefault(u => u.Id == id);
            if (orderMaterial == null)
            {
                return false;
            }

            return _orderMaterialDal.DeleteEntity(orderMaterial);
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteOrderMaterials(List<string> ids)
        {
            foreach (var id in ids)
            {
                OrderMaterial orderMaterial = _orderMaterialDal.GetDbSet().FirstOrDefault(u => u.Id == id);
                if (orderMaterial == null)
                {
                    return false;
                }

                _orderMaterialDal.DeleteEntity(orderMaterial);
            }

            return true;
        }


        /// <summary>
        /// 获取订单的备选材料集合
        /// </summary>
        /// <returns></returns>
        public object GetMaterialList(string orderId, string title, int page, int limit, out int count)
        {
            //获取此订单下已经绑定了哪些材料
            List<string> materialIds = _orderMaterialDal.GetDbSet().Where(o => o.OrderId == orderId).Select(o => o.MaterialId).ToList();

            var materialSet = _materialDal.GetDbSet().Where(n => !n.IsDelete);

            if (materialIds.Count != 0)
            {
                //绑定过的就不要，也就是查询没绑定过的
                materialSet = materialSet.Where(m => !materialIds.Contains(m.Id));
            }

            //左连接
            var queryable = from n in materialSet  //材料
                            join c in _classificationDal.GetDbSet().Where(u => !u.IsDelete)
                            on n.ClassificationId equals c.Id into cTemp
                            from c2 in cTemp.DefaultIfEmpty()
                            select new
                            {
                                n.Id,
                                n.Title,
                                n.CreateTime,
                                n.Unit,
                                n.Price,
                                n.PriceMain,
                                n.PriceAssistant,
                                n.PriceMachine,
                                n.PriceArtificial,
                                n.Remark,
                                n.PriceLoss,
                                Number = 1,
                                ClassificationTitle = c2.Title
                            };


            //IQueryable<Material> queryable = _materialDal.GetMaterialsDbSet().Where(u => !u.IsDelete).OrderByDescending(d => d.CreateTime);

            if (!string.IsNullOrEmpty(title))
            {
                queryable = queryable.Where(u => u.Title.Contains(title));
            }


            //查询总条数
            count = queryable.Count();

            var list = queryable.OrderByDescending(d => d.CreateTime).Skip((page - 1) * limit).Take(limit).ToList();

            return list;
        }


        ///// <summary>
        ///// 根据订单材料id获取订单材料信息
        ///// </summary>
        ///// <param name="orderMaterialId"></param>
        ///// <returns></returns>
        //public object GetOrderMaterialById(string orderMaterialId)
        //{
        //    var data = (from n in _orderMaterialDal.GetDbSet().Where(n => !n.IsDelete)  //订单材料
        //                join u in _userInfoDal.GetDbSet().Where(u => !u.IsDelete)
        //                on n.CreatorId equals u.Id into uTemp
        //                from u2 in uTemp.DefaultIfEmpty()
        //                select new
        //                {
        //                    n.Id,
        //                    n.Title,
        //                    n.CreateTime,
        //                    n.Content,
        //                    Creator = u2.UserName
        //                }).FirstOrDefault(n => n.Id == orderMaterialId);

        //    if (data != null)
        //    {
        //        return new
        //        {
        //            data.Content,
        //            data.Id,
        //            data.Title,
        //            CreateTime = data.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
        //            data.Creator
        //        };
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}


        /// <summary>
        /// 获取订单材料集合
        /// </summary>
        public object GetOrderMaterialList(string orderId, string title, int page, int limit, out int count)
        {
            var queryable = from om in _orderMaterialDal.GetDbSet().Where(om => om.OrderId == orderId)
                            join m in _materialDal.GetDbSet().Where(m => !m.IsDelete)
                            on om.MaterialId equals m.Id
                            join c in _classificationDal.GetDbSet().Where(c => !c.IsDelete)
                            on m.ClassificationId equals c.Id
                            select new
                            {
                                om.Id,
                                m.Title,
                                om.Num,
                                m.Unit,
                                m.Price,
                                m.Remark,
                                ClassificationTitle = c.Title,
                                m.CreateTime
                            };

            if (!string.IsNullOrEmpty(title))
            {
                queryable = queryable.Where(u => u.Title.Contains(title));
            }


            //查询总条数
            count = queryable.Count();

            var list = queryable.OrderByDescending(d => d.CreateTime).Skip((page - 1) * limit).Take(limit).ToList();

            return list;
        }



        ///// <summary>
        ///// 更新订单材料
        ///// </summary>
        ///// <returns></returns>
        //public bool UpdateOrderMaterial(string orderMaterialId, string title, string content, out string msg)
        //{
        //    //根据账号找出订单材料的信息
        //    OrderMaterial entity = _orderMaterialDal.GetDbSet().FirstOrDefault(u => u.Id == orderMaterialId);
        //    if (entity == null)
        //    {
        //        msg = "未找到订单材料信息";
        //        return false;
        //    }

        //    //查询是否修改到其他的名字一样的情况
        //    OrderMaterial otherEntity = _orderMaterialDal.GetDbSet().FirstOrDefault(u => u.Id != orderMaterialId && u.Title == title);
        //    if (otherEntity != null)
        //    {
        //        msg = "此订单材料名字已存在";
        //        return false;
        //    }

        //    entity.Title = title;
        //    entity.Content = content;

        //    //调用数据访问层
        //    bool isSucess = _orderMaterialDal.UpdateEntity(entity);
        //    msg = "成功";
        //    return isSucess;
        //}


    }
}